package com.barradata.app0624.modules.contact

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.barradata.R
import com.barradata.common.base.BaseActivity
import com.barradata.common.utils.EditTextUtils
import com.example.hfiveandaplication.bean.ContactItemInfoData
import com.jakewharton.rxbinding3.widget.textChanges
import kotlinx.android.synthetic.main.contact_rv_item.view.*

/**
 * Author:    WuNan
 * Email:     geek5nan@gmail.com
 * Created:   2019-07-13 22:02
 **/
class ContactAdapter(val context: BaseActivity,
                     private val contactList: MutableList<ContactItemInfoData>,
                     private val phoneButtonAction: (Int) -> Unit,
                     private val relationButtonAction: (Int) -> Unit,
                     val nextButtonAction: () -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 0) {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.contact_rv_item, parent, false)
            return ContactItemHolder(view)
        } else {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.contact_rv_footer, parent, false)
            return FooterViewHolder(view)
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (position < contactList.size) {
            return 0
        }
        return 1
    }

    override fun getItemCount() = contactList.size + 1

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ContactItemHolder) {
            holder.fillData(contactList[position])
            holder.bindEvent(context, contactList, phoneButtonAction, relationButtonAction)
        }
        if (holder is FooterViewHolder) {
            holder.itemView.setOnClickListener { nextButtonAction() }
        }
    }

    class ContactItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindEvent(context: BaseActivity, contactItemList: MutableList<ContactItemInfoData>, phoneButtonAction: (Int) -> Unit, relationButtonAction: (Int) -> Unit) {
            itemView.apply {
                // 点击 itemView 时，清除 editText 的焦点
                setOnClickListener {
                    EditTextUtils.hideKeyboard(context)
                    contact_name_et.clearFocus()
                }
                // 设置 选择联系人及选择关系 回调
                contact_phone_button.setOnClickListener { phoneButtonAction(adapterPosition) }
                contact_relation_button.setOnClickListener { relationButtonAction(adapterPosition) }
                // RxBinding 订阅 editText 的 textChange 事件，文本变更的同时更新数据模型
                val disposable = contact_name_et.textChanges().subscribe {
                    contactItemList[adapterPosition].userName = it.toString()
                }
                context.compositeDisposable()?.add(disposable)
            }
        }

        fun fillData(data: ContactItemInfoData) {
            itemView.apply {
                contact_title.text = context.getString(R.string.contact_auth_contact).plus(adapterPosition + 1)
                if (!data.mobile.isNullOrBlank()) {
                    contact_phone_button.text = data.mobile
                }
                if (!data.userName.isNullOrBlank()) {
                    contact_name_et.setText(data.userName)
                }
                if (!data.realtionValue.isNullOrBlank()) {
                    contact_relation_button.text = data.realtionValue
                }
            }
        }
    }

    class FooterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}